package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

public class LeetCodeTest0337 {

    public static void main(String[] args) {

    }

    public int rob(TreeNode root) {
        int [] res = dfs(root);
        return Math.max(res[0], res[1]);
    }

    int [] dfs(TreeNode root){
        if(root == null){
            return new int[2];
        }
        int [] leftCalculate = dfs(root.left);
        int [] rightCalculate = dfs(root.right);
        int selected = root.val + leftCalculate[1] + rightCalculate[1]; // 0表示root被选中，1表示root不被选中；root被选中情况下，其左右孩子都不能被选中
        int noSelected = Math.max(leftCalculate[0], leftCalculate[1]) + Math.max(rightCalculate[0], rightCalculate[1]);
        return new int[] {selected, noSelected};
    }
}
